package edu.gatech.coc.betameisters.userinterfaces;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * This is the graphing class. It is just a panel for the patients weight and height
 * @author Henry
 *
 */
public class Graph extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 100846306990484291L;
	private double[] weights;
	private double[] heights;
	
	private final int WIDTH = 274;
	private final int HEIGHT = 306;
	
	/**
	 * Constructor for graph
	 * It is important that weights and heights are the same length.
	 * @param weights the patients weights, in order from earliest to latest
	 * @param heights the patients heights, in order from earliest to latest.
	 */
	public Graph(double[] weights, double[] heights)
	{
		this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		this.weights = weights;
		this.heights = heights;
	}
	
	/**
	 * The paintComponent, to draw all weights and heights
	 */
	public void paintComponent(Graphics g)
	{
		g.drawLine(50, 50, 50, HEIGHT-50);
		g.drawLine(50, HEIGHT-50, WIDTH-50, HEIGHT-50);
		g.drawLine(WIDTH-50, 50, WIDTH-50, HEIGHT-50);
		g.setColor(Color.BLUE);
		g.drawString("Weights", 0, HEIGHT/2);
		g.setColor(Color.RED);
		g.drawString("Heights", WIDTH-45, HEIGHT/2);
		g.setColor(Color.BLACK);
		g.drawString("Time",WIDTH/2-10, HEIGHT-15);
		double minWeight = Double.MAX_VALUE;
		double maxWeight = Double.MIN_VALUE;
		for(int i = 0;i<weights.length;i++)
		{
			if(weights[i]<minWeight)
				minWeight = weights[i];
			if(weights[i]>maxWeight)
				maxWeight = weights[i];
		}
		double minHeight = Double.MAX_VALUE;
		double maxHeight = Double.MIN_VALUE;
		for(int i = 0;i<heights.length;i++)
		{
			if(heights[i]<minHeight)
				minHeight = heights[i];
			if(heights[i]>maxHeight)
				maxHeight = heights[i];
		}
		g.drawString(Integer.toString((int)minWeight), 30, HEIGHT-50);
		g.drawString(Integer.toString((int)maxWeight), 30, 50);
		g.drawString(Integer.toString((int)(minWeight+maxWeight)/2), 30, HEIGHT/2 + 20);
		g.drawString(Integer.toString((int)minHeight), WIDTH-30, HEIGHT-50);
		g.drawString(Integer.toString((int)maxHeight), WIDTH-30, 50);
		g.drawString(Integer.toString((int)(minHeight+maxHeight)/2), WIDTH-30, HEIGHT/2 + 20);
		int xLoc = 50;
		int xPrev = 0;
		for(int i = 0; i<weights.length;i++)
		{
			g.setColor(Color.BLACK);
			g.drawString(Integer.toString(i+1), xLoc, HEIGHT-35);
			g.setColor(Color.BLUE);
			g.drawOval(xLoc,  findY(minWeight, maxWeight, weights[i]), 3, 3);
			g.setColor(Color.RED);
			g.drawOval(xLoc,  findY(minHeight, maxHeight, heights[i]), 3, 3);
			
			if(i!=0)
			{
				g.setColor(Color.RED);
				g.drawLine(xLoc, findY(minHeight, maxHeight, heights[i]), xPrev, findY(minHeight, maxHeight, heights[i-1]));
				g.setColor(Color.BLUE);
				g.drawLine(xLoc, findY(minWeight, maxWeight, weights[i]), xPrev, findY(minWeight, maxWeight, weights[i-1]));
			}
			xPrev = xLoc;
			xLoc += (WIDTH-100)/weights.length;
		}
		
	}
	
	/**
	 * A helper function I needed to find the given Y coordinate for a weight or height
	 * @param min minHeight or minWeight, depending on which we're talking about.
	 * @param max maxHeight or maxWeight, depending on which we're talking about.
	 * @param toFind the weight or height we need a Y value for
	 * @return
	 */
	private int findY(double min, double max, double toFind)
	{
		double outOfOne = (double)(toFind-min)/(max-min);
		double inversed = 1-outOfOne;
		double scaled = inversed*(HEIGHT-100);
		return (int)scaled+50;
	}
	
	/**
	 * This was just so I could test the panel
	 * @param args
	 */
	public static void main(String[] args)
	{
		
		double[] weights = {10,20,4,5,25,60,43,23};
		double[] heights = {30,22,1,17,19,53,49,12};
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(new Graph(weights, heights));
		frame.setVisible(true);
	}

}
